package com.hzf.ucenter.admin.controller.manage;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.hzf.common.base.BaseController;
import com.hzf.common.util.StringUtil;
import com.hzf.common.validator.LengthValidator;
import com.hzf.ucenter.dao.model.UcenterUser;
import com.hzf.ucenter.dao.model.UcenterUserExample;
import com.hzf.ucenter.rpc.api.UcenterUserService;
import com.hzf.ucenter.rpc.common.constant.UcenterResult;
import com.hzf.ucenter.rpc.common.constant.UcenterResultConstant;
import com.hzf.upms.common.constant.UpmsResult;
import com.hzf.upms.common.constant.UpmsResultConstant;
import com.hzf.upms.dao.model.UpmsRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
*  平台基础用户管理
*  Created by JiangXueLi on 2017/11/08.
*
*/

@Controller
@Api(value = "平台基础用户管理", description = "基础用户管理")
@RequestMapping("/manage/ucenterUser")
public class UcenterUserController extends BaseController {
    private static Logger _log = LoggerFactory.getLogger(UcenterUserController.class);
    @Autowired
    private UcenterUserService ucenterUserService;

    @ApiOperation(value = "基础用户首页")
    @RequiresPermissions("ucenter:user:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/user/index.jsp";
    }
    @ApiOperation(value="基础用户列表")
    @RequiresPermissions("ucenter:user:read")
    @RequestMapping(value="/list",method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order){
        UcenterUserExample ucenterUserExample = new UcenterUserExample();
        if(!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)){
            ucenterUserExample.setOrderByClause(sort+" "+order);
        }
        List<UcenterUser> rows =ucenterUserService.selectByExampleForOffsetPage(ucenterUserExample,offset,limit);
        long total = ucenterUserService.countByExample(ucenterUserExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }
    @ApiOperation(value = "新增用户")
    @RequiresPermissions("ucenter:user:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(ModelMap modelMap) { return "/manage/user/create.jsp"; }
    @ApiOperation(value = "新增用户")
    @RequiresPermissions("ucenter:user:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(UcenterUser ucenterUser) {
        ComplexResult result = FluentValidator.checkAll()
                .on(ucenterUser.getUserName(), new LengthValidator(1, 30, "昵称"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new UcenterResult(UcenterResultConstant.INVALID_LENGTH, result.getErrors());
        }
        long time = System.currentTimeMillis();
        ucenterUser.setUserNumber(UUID.randomUUID().toString().replaceAll("-", ""));
        ucenterUser.setCreateDate(time);
        ucenterUser.setUpdateTime(time);
        int count = ucenterUserService.insertSelective(ucenterUser);
        return new UcenterResult(UcenterResultConstant.SUCCESS, count);
    }
    @ApiOperation(value = "删除用户")
    @RequiresPermissions("ucenter:user:delete")
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = ucenterUserService.deleteByPrimaryKeys(ids);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改用户")
    @RequiresPermissions("ucenter:user:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        UcenterUser ucenterUser = ucenterUserService.selectByPrimaryKey(id);
        modelMap.put("ucenterUser", ucenterUser);
        System.out.println("返回了");
        return "/manage/user/update.jsp";
    }

    @ApiOperation(value = "修改用户")
    @RequiresPermissions("ucenter:user:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") int id, UcenterUser ucenterUser) {
        ComplexResult result = FluentValidator.checkAll()
                .on(ucenterUser.getUserName(), new LengthValidator(1, 20, "用户名"))
                .on(ucenterUser.getPassword(), new LengthValidator(1, 20, "用户密码"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
        }
        ucenterUser.setId(id);
        int count = ucenterUserService.updateByPrimaryKeySelective(ucenterUser);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

}
